package com.mcarthur.countdown.client.drawable;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.gwtgl.binding.WebGLRenderingContext;

public class Digit extends AbstractDrawable {
	public static final int WIDTH = 71;
	
	private static final int ROWS = 7;
	private static final int COLS = 4;
	private static final int BALL_SEP = 5;
	
	private static final byte[] zero = {
		1, 1, 1, 1,
		1, 0, 0, 1,
		1, 0, 0, 1,
		1, 0, 0, 1,
		1, 0, 0, 1,
		1, 0, 0, 1,
		1, 1, 1, 1
	};
	
	private static final byte[] one = {
		0, 0, 0, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		0, 0, 0, 1
	};
	
	private static final byte[] two = {
		1, 1, 1, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		1, 1, 1, 1,
		1, 0, 0, 0,
		1, 0, 0, 0,
		1, 1, 1, 1
	};
	
	private static final byte[] three = {
		1, 1, 1, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		1, 1, 1, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		1, 1, 1, 1
	};
	
	private static final byte[] four = {
		1, 0, 0, 1,
		1, 0, 0, 1,
		1, 0, 0, 1,
		1, 1, 1, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		0, 0, 0, 1
	};
	
	private static final byte[] five = {
		1, 1, 1, 1,
		1, 0, 0, 0,
		1, 0, 0, 0,
		1, 1, 1, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		1, 1, 1, 1
	};
	
	private static final byte[] six = {
		1, 1, 1, 1,
		1, 0, 0, 0,
		1, 0, 0, 0,
		1, 1, 1, 1,
		1, 0, 0, 1,
		1, 0, 0, 1,
		1, 1, 1, 1
	};
	
	private static final byte[] seven = {
		1, 1, 1, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		0, 0, 0, 1
	};
	
	private static final byte[] eight = {
		1, 1, 1, 1,
		1, 0, 0, 1,
		1, 0, 0, 1,
		1, 1, 1, 1,
		1, 0, 0, 1,
		1, 0, 0, 1,
		1, 1, 1, 1
	};
	
	private static final byte[] nine = {
		1, 1, 1, 1,
		1, 0, 0, 1,
		1, 0, 0, 1,
		1, 1, 1, 1,
		0, 0, 0, 1,
		0, 0, 0, 1,
		1, 1, 1, 1
	};
	
	private List<Ball> mBalls;
	
	public Digit(float posX, Color activeColor) {
		super(posX);
		mBalls = new ArrayList<Ball>();
		
		for (int r = 0; r < ROWS; r++) {
			float currentX = posX;
			for (int c = 0; c < COLS; c++) {
				
				// added in row major order
				mBalls.add(new Ball(currentX, activeColor));
				currentX += BALL_SEP + Ball.WIDTH;
			}
		}
		setDigit('0');
	}

	public void drawInactive(WebGLRenderingContext gl) {
	    for (Ball ball : mBalls) {
            if (!ball.isActive()) {
                ball.draw(gl);
            }
        }
	}
	
	public void draw(WebGLRenderingContext gl) {
	    for (Ball ball : mBalls) {
	        if (ball.isActive()) {
	            ball.draw(gl);
	        }
	    }
//		for (int i = 0; i < mBalls.size(); i++) {
//			mBalls.get(i).draw(gl);
//		}
	}
	
	@Override
	public void setPosY(float posY) {
		super.setPosY(posY);
		for (int c = 0; c < COLS; c++) {
			float currentY = posY;
			for (int r = ROWS - 1; r >= 0; r--) {
				mBalls.get((r*COLS) + c).setPosY(currentY);
				currentY += BALL_SEP + Ball.WIDTH;
			}
		}
	}
	
	public void setDigit(char digit) {
		byte[] currentDigit = zero;
		switch(digit) {
		case '0':
			//currentDigit = zero;
			break;
		case '1':
			currentDigit = one;
			break;
		case '2':
			currentDigit = two;
			break;
		case '3':
			currentDigit = three;
			break;
		case '4':
			currentDigit = four;
			break;
		case '5':
			currentDigit = five;
			break;
		case '6':
			currentDigit = six;
			break;
		case '7':
			currentDigit = seven;
			break;
		case '8':
			currentDigit = eight;
			break;
		case '9':
			currentDigit = nine;
			break;
		}

		for (int i = 0; i < currentDigit.length; i++) {
			boolean nowActive = currentDigit[i] > 0;
			Ball ball = mBalls.get(i);
			
			//TODO: investigate explosive drop-to-nine behavior
			if (ball.isActive() && !nowActive) {
				// BOUNCY BALL TIME!
				BouncyBallManager.get().activateBouncyBall(ball);
			}
			ball.setActive(nowActive);
		}
	}
}
